package cn.wolfcode.trip.app.controller;


import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyCatalogQuery;
import cn.wolfcode.trip.base.query.StrategyCommentQuery;
import cn.wolfcode.trip.base.query.StrategyQuery;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//攻略资源控制器
@RestController
@RequestMapping("/strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyCatalogService catalogService;

    @Autowired
    private IStrategyCommentService commentService;

    @Autowired
    private ITagService tagService;

    @Autowired
    private ITravelService travelService;

    //根据状态/地区id查出所有的攻略
    @GetMapping
    public PageInfo query(StrategyQuery qo){
        PageInfo<Strategy> query = strategyService.query(qo);
        return query;
    }

    //根据攻略id查出改攻略的目录
    @GetMapping("/{strategyId}/strategycatalog")
    public PageInfo selectCatalogByStrategyId(StrategyCatalogQuery qo){
        qo.setPageSize(0);
        qo.setOrderBy("sc.sequence");
        return catalogService.query(qo);
    }

    //根据攻略id查出攻略
    @GetMapping("/{strategyId}")
    public Strategy selectStrategyById(@PathVariable Long strategyId){
        return strategyService.selectByStrategyById(strategyId);
    }

    //根据攻略id查出该攻略的点评
    @GetMapping("/{strategyId}/comments")
    public PageInfo<StrategyComment> selectStrategyCommentById(StrategyCommentQuery qo){
        qo.setOrderBy("sc.createTime desc");
        return commentService.query(qo);
    }

    //保存评论到该攻略
    @PostMapping("/{strategy.id}/comments")
    public JSONResult save(StrategyComment comment,String[] tags){
        JSONResult jsonResult = new JSONResult();
        try {
            commentService.save(comment,tags);
        } catch (Exception e) {
            jsonResult.mark("保存失败,稍后再试");
            e.printStackTrace();
        }

        return jsonResult;
    }

    //根据攻略id查出该攻略的热门标签
    @GetMapping("/{strategyId}/tags")
    public List<Tag> selectHotTagsBystrategyId(@PathVariable Long strategyId){

        return tagService.selectHotTagsBystrategyId(strategyId);
    }


    /** qiuyanquan
     * 判断用户是否收藏该文章
     * @param travelId
     * @param userId
     * @return
     */
    @GetMapping("/collectionCount")
    public  Integer getCollectionById( Long travelId, Long userId,int type){
        return travelService.getCollectionById(travelId,userId,type);
    }


    //收藏攻略
    @PostMapping("/collection")
    public JSONResult save(Long travelId,Long userId,int type) {
        travelService.save(travelId,userId,type);
        return new JSONResult();
    }



    //取消收藏
    @DeleteMapping("/{id}")
    public JSONResult delete(@PathVariable Long id) {
        Long userId = UserContext.getCurrentUser().getId();
        travelService.delete(id,userId,2);
        return new JSONResult();
    }



}
